#include "rsp_watch.hpp"
#include "../action_constants.hpp"

namespace etcdv3 {
    
void AsyncWatchResponse::ParseResponse(WatchResponse& reply) {
    index = reply.header().revision();
    for (int cnt = 0; cnt < reply.events_size(); cnt++) {
        auto event = reply.events(cnt);
        if (mvccpb::Event::EventType::Event_EventType_PUT == event.type()) {
            if (event.kv().version() == 1) {
                action = CREATE_ACTION;
            } else {
                action = SET_ACTION;
            }
            value.kvs = event.kv();

        } else if (mvccpb::Event::EventType::Event_EventType_DEL == event.type()) {
            action = DELETE_ACTION;
            value.kvs = event.kv();
        }
        if (event.has_prev_kv()) {
            prev_value.kvs = event.prev_kv();
        }
        // just store the first occurence of the key in values.
        // this is done so tas client will not need to change their behaviour.
        // break immediately
        break;
    }
}

}

